package com.czxy.tanhua.interceptor;

import com.czxy.tanhua.entity.User;
public class UserHolder {
    // 创建一个ThreadLocal类
    private static ThreadLocal<User> threadLocal = new ThreadLocal<>();

    /**
     * 保存用户信息
     * @param user
     */
    public static void set(User user) {
        threadLocal.set(user);
    }

    /**
     * 获得线程中用户信息
     * @return
     */
    public static User get() {
        return threadLocal.get();
    }

    /**
     * 获得用户id
     * @return
     */
    public static Long getUserId() {
        User user = get();
        if(user == null) {
            return null;
        }
        return user.getId();
    }

    /**
     * 从当前线程，获取用户对象的手机号码
     */
    public static String getMobile() {
        if (threadLocal.get() == null) {
            return null;
        }
        return threadLocal.get().getMobile();
    }

    /**
     * 移除线程中数据
     */
    public static void remove() {
        threadLocal.remove();
    }
}
